package com.vdong.supplier.service.impl.supplierbird;

import com.fasterxml.jackson.databind.JsonNode;
import com.vdong.supplier.http.PostQuerClassServiceHttpClient;
import com.vdong.supplier.http.TestPostQuerClassServiceHttpClient;
import com.vdong.supplier.request.supplierbird.EbusinessOrderHandleRequest;
import com.vdong.supplier.response.supplierbird.*;
import com.vdong.supplier.service.supplierbird.PostQueryClassService;
import com.vdong.supplier.util.ObjectMapperSingleton;
import com.vdong.supplier.util.Post100Md5Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PostQueryClassServiceImpl implements PostQueryClassService {

    private Logger logger = LoggerFactory.getLogger(PostQueryClassServiceImpl.class);
    @Autowired
    private PostQuerClassServiceHttpClient postQuerClassServiceHttpClient;
    @Autowired
    private TestPostQuerClassServiceHttpClient testPostQuerClassServiceHttpClient;

    public KdnResponse EbusinessOrderHandle(EbusinessOrderHandleRequest ebusinessOrderHandleRequest) {

        String json;
        try {
            json = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                    .writeValueAsString(ebusinessOrderHandleRequest.getRequestData());

            String response = postQuerClassServiceHttpClient.EbusinessOrderHandleHttpClient(json,
                    ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                    Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                    ebusinessOrderHandleRequest.getDataType());

            JsonNode jsonResponse = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap().readTree(response);
            Boolean success = jsonResponse.get("Success").asBoolean();

            if (success) {
                logger.info(ebusinessOrderHandleRequest.getRequestType() + ":begin:" + response);
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setResponseParent(ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                        .treeToValue(jsonResponse, EbusinessOrderHandleResponse.class));

                return kdn;
            } else {
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setMessage(jsonResponse.get("Reason").asText());
                return kdn;
            }
        } catch (Exception e) {

            logger.error(ebusinessOrderHandleRequest.getRequestType() + ":end:" + e + ",message" + e.getMessage());

        }
        return null;

    }

    public KdnResponse EbusinessOrder(EbusinessOrderHandleRequest ebusinessOrderHandleRequest, int environment) {

        String json;
        try {
            json = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                    .writeValueAsString(ebusinessOrderHandleRequest.getRequestData());
            String response = null;
            if (environment == 0) {
                response = testPostQuerClassServiceHttpClient.EbusinessOrderClient(json,
                        ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                        Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                        ebusinessOrderHandleRequest.getDataType());
            } else {
                response = postQuerClassServiceHttpClient.EbusinessOrderClient(json,
                        ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                        Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                        ebusinessOrderHandleRequest.getDataType());
            }

            JsonNode jsonResponse = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap().readTree(response);
            Boolean success = jsonResponse.get("Success").asBoolean();

            if (success) {
                logger.info(ebusinessOrderHandleRequest.getRequestType() + ":begin:" + response);
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setResponseParent(ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                        .treeToValue(jsonResponse, EbusinessOrderResponse.class));

                return kdn;
            } else {
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setMessage(jsonResponse.get("Reason").asText());
                return kdn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ebusinessOrderHandleRequest.getRequestType() + ":end:" + e + ",message" + e.getMessage());
        }

        return null;
    }

    @Override
    public KdnResponse EbusinessOOrder(EbusinessOrderHandleRequest ebusinessOrderHandleRequest, int environment) {

        String json;
        try {
            json = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                    .writeValueAsString(ebusinessOrderHandleRequest.getRequestData());

            String response = null;
            if (environment == 0) {
                response = testPostQuerClassServiceHttpClient.EbusinessOOrderClient(json,
                        ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                        Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                        ebusinessOrderHandleRequest.getDataType());
            } else {
                response = postQuerClassServiceHttpClient.EbusinessOOrderClient(json,
                        ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                        Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                        ebusinessOrderHandleRequest.getDataType());
            }



            JsonNode jsonResponse = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap().readTree(response);
            Boolean success = jsonResponse.get("Success").asBoolean();

            if (success) {
                logger.info(ebusinessOrderHandleRequest.getRequestType() + ":begin:" + response);
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setResponseParent(ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                        .treeToValue(jsonResponse, EbusinessOOrderResponse.class));

                return kdn;
            } else {
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setMessage(jsonResponse.get("Reason").asText());
                return kdn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ebusinessOrderHandleRequest.getRequestType() + ":end:" + e + ",message" + e.getMessage());
        }

        return null;
    }

    @Override
    public KdnResponse EbusinessCancleOrder(EbusinessOrderHandleRequest ebusinessOrderHandleRequest, int environment) {
        String json;
        try {
            json = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                    .writeValueAsString(ebusinessOrderHandleRequest.getRequestData());
            String response = null;
            if (environment == 0) {
                response = testPostQuerClassServiceHttpClient.EbusinessOrderHandleClient(json,
                        ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                        Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                        ebusinessOrderHandleRequest.getDataType());
            } else {
                response = postQuerClassServiceHttpClient.EbusinessOrderHandleClient(json,
                        ebusinessOrderHandleRequest.geteBusinessID(), ebusinessOrderHandleRequest.getRequestType(),
                        Post100Md5Base64.encrypt(json, ebusinessOrderHandleRequest.getAppkey(), "UTF-8"),
                        ebusinessOrderHandleRequest.getDataType());
            }

            logger.info(ebusinessOrderHandleRequest.getRequestType() + ":begin:" + response);
            JsonNode jsonResponse = ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap().readTree(response);
            Boolean success = jsonResponse.get("Success").asBoolean();

            if (success) {

                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setResponseParent(ObjectMapperSingleton.ObjectMapperCustom.getJsonObjectMap()
                        .treeToValue(jsonResponse, EbusinessCancelOrderResponse.class));

                return kdn;
            } else {
                KdnResponse kdn = new KdnResponse();
                kdn.setSuccess(success);
                kdn.setMessage(jsonResponse.get("Reason").asText());
                return kdn;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ebusinessOrderHandleRequest.getRequestType() + ":end:" + e + ",message" + e.getMessage());
        }

        return null;
    }

}
